<template>
  <view>
    <view class="search-box">
      <uni-search-bar
        @input="input"
        :radius="15"
        placeholder="请输入搜索内容"
        cancelButton="none"
        clearButton="auto"
        focus="always"
        ></uni-search-bar>
    </view>
    <!-- 搜索数据列表 -->
    <view v-if="goodsList.length !== 0" class="search-list" >
      <view class="list" v-for="(item, i) in goodsList" :key="i" @click="gotoDetail(item.goods_id)">
        <view class="good-name">{{item.goods_name}}</view>
        <uni-icons type="arrow-right" size="16"></uni-icons>
      </view>
    </view>
    <!-- 搜索历史记录 -->
    <view v-else class="history-box">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash-filled" size="20" @click="delHistory"></uni-icons>
      </view>
      <!-- 历史记录-->
      <view class="history-list">
        <uni-tag inverted="true" :text="item" v-for="(item, i) in histories" :key="i" @click="gotoGoodsList(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        timer: null,
        // 搜索的关键字
        keyWorld: '',
        // 搜索历史的数据
        historyList: [],
        // 搜索的数据
        goodsList: []
      };
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('keyWorld') || '[]')
    },
    methods: {
      // input输入事件
      input(e) {
        // 进行防抖处理
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.keyWorld = e
          // 触发获取搜索数据的方法
          this.getSearchList()
          // 向历史记录加入数据
          this.saveHistoryData()
        }, 500)
      },
      // 获取搜索数据的方法
      async getSearchList() {
        // 非空判断
        if (this.keyWorld === '') {
          this.goodsList = []
          return
        }
        const { data: res } = await uni.$http.get(`/api/public/v1/goods/qsearch`, { query: this.keyWorld })
        this.goodsList = res.message
        if (res.meta.status !== 200) return uni.$showMsg()
      },
      // 点击跳转商品详情页
      gotoDetail(item) {
        uni.navigateTo({
          url: `/subpkg/goods_detail/goods_detail?goods_id=${item}`
        })
      },
      // 输入完后向历史记录推送数据
      saveHistoryData() {
        // this.historyList.push(this.keyWorld)
        const set = new Set(this.historyList)
        set.delete(this.keyWorld)
        if (this.keyWorld === '') return
        set.add(this.keyWorld)
        this.historyList = Array.from(set)
        // 对搜索历史记录数据进行持久化处理
        uni.setStorageSync('keyWorld', JSON.stringify(this.historyList))
      },
      // 点击清空历史记录方法
      delHistory() {
        this.historyList = []
        uni.setStorageSync('keyWorld', [])
        // 第二种方法
        // uni.removeStorageSync('keyWorld')
      },
      // 点击历史记录跳转至商品列表页面
      gotoGoodsList(item) {
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?query=${item}`
        })
      }
    },
    computed: {
      histories() {
        return [...this.historyList].reverse()
      }
    }
  }
</script>

<style lang="scss" scoped>
.search-box{
  position: sticky;
  top: 0;
  z-index: 999;
  background-color: #c00000;
}
.search-list{
  margin: 0 5px;
  .list{
    display: flex;
    align-items: center;
    justify-content: space-between;
    font-size: 12px;
    padding: 13px 0;
    border-bottom: 1px solid #ccc;
    .good-name {
      // 文本溢出处理
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;
      margin-right: 3px;
    }
  }
}
// 搜索历史记录
.history-box{
  .history-title{
    height: 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 13px;
    padding: 0 10px;
  }
  .history-list{
    margin-left: 10px;
    display: flex;
    flex-wrap: wrap;
    uni-tag{
      margin-top: 5px;
      margin-right: 5px;
    }
  }
}
</style>
